package com.mtcarpenter.lambda;


import org.junit.Test;

import java.util.Comparator;

/**
 * @Author: mtcarpenter
 * @describe 内部匿名函数
 * @Date: 2019/10/5
 * @Version 1.0
 */
public class AnonymousTest {

    /**
     * 范例 1 ：无参内部匿名类
     */
    @Test
    public void test01(){
        System.out.println("*************  JDK 1.7 *****************");
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("学习啦！学习啦！");
            }
        };
        runnable.run();

        System.out.println("************  JDK 1.8 Lambda ************");
        Runnable runnable2 = () -> {
            System.out.println("学习啦！学习啦！！");
        };
        runnable2.run();
    }

    /**
     * 范例 2 ：无有参数内部匿名类
     */
    @Test
    public void test02(){
        System.out.println("****************  JDK 1.7 比较函数   ********************");
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        int compare = comparator.compare(10, 20);
        System.out.println(compare);

        System.out.println(" ************** JDK 1.8 比较函数 Lambda  ****************");
        // Lambda 表达式
        Comparator<Integer> comp1 = (o1, o2) -> Integer.compare(o1, o2);
        int comparator2 = comp1.compare(10,20);
        System.out.println(comparator2);

        System.out.println("************** JDK 1.8 比较函数 方法引用  ****************");
        // 方法引用
        Comparator<Integer>  comp2=  Integer::compare;
        int compare1 = comp2.compare(10, 20);
        System.out.println(compare1);
    }
}
